const app = getApp()
const {
	getOrder,
	changeOrder,
	finishOrder,
	finishEvaluate
} = require('../../utils/api')
const {
	returnBack
} = require('../../utils/util')

Page({
	data: {
		//当前步骤（共用）
		stepIndex: null,

		/**
		 * 陪诊预约
		 */
		steps: [
			{
				stepId: -1,
				stepName: '未支付'
			},
			{
				stepId: 0,
				stepName: '等待接单'
			},
			{
				stepId: 1,
				stepName: '已被接单'
			},
			{
				stepId: 2,
				stepName: '陪诊中'
			},
			{
				stepId: 3,
				stepName: '待确认'
			},
			{
				stepId: 4,
				stepName: '已完成'
			},
		],

		/**
		 * 送取服务
		 */
		sendSteps: [
			{
				stepId: -1,
				stepName: '未支付'
			},
			{
				stepId: 0,
				stepName: '等待接单'
			},
			{
				stepId: 1,
				stepName: '已被接单'
			},
			{
				stepId: 2,
				stepName: '派送中'
			},
			{
				stepId: 3,
				stepName: '待确认'
			},
			{
				stepId: 4,
				stepName: '已完成'
			},
		],

		/**
		 * 陪护预约
		 */
		chaperonageSteps: [
			{
				stepId: -1,
				stepName: '未支付'
			},
			{
				stepId: 0,
				stepName: '等待接单'
			},
			{
				stepId: 1,
				stepName: '已被接单'
			},
			{
				stepId: 2,
				stepName: '陪护中'
			},
			{
				stepId: 3,
				stepName: '待确认'
			},
			{
				stepId: 4,
				stepName: '已完成'
			},
		],

		currentUser: '', //当前登录用户
		orderId: null, //订单id
		orderDetail: {
			id: 13,
			userId: 3, // 下单用户id
			userName: null, // 下单用户姓名
			userTel: "15726119238", // 下单用户联系方式

			//统一显示（待支付时）
			status: "5", // 订单状态：根据该属性判断展示内容
			orderNumber: "XS202402021031439410070", // 订单号
			orderTypeId: 1, //订单类型
			createTime: "2024-02-02T10:31:44", // 创建时间
			memberName: "祖晟伦", // 患者姓名
			memberTel: "15606403947", // 患者联系方式
			memberDetail: "需要车接送", // 患者备注
			clinicTime: "2021-12-23", // 就诊时间
			hospital: "省立二院", // 就诊医院

			//已支付-等待接单（status = 0）
			money: 100, // 订单金额

			//已被接单-等待服务开始（status = 1)
			workerId: null, // 陪诊员id
			workerName: null, // 陪诊员姓名
			workerTel: null, // 陪诊员联系方式
			isAccept: null, // 是否被接单

			//陪诊中（status = 2)

			//服务完成，待患者确认（status = 3）
			situation: "情况硚口区客气客气客气客气", // 患者情况
			suggestion: null, // 医嘱

			//已完成（status = 4)
			finishTime: null, // 完成时间

			//已评价（status = 5)
			memberEva: null, // 用户评价
			workerEva: null, // 陪诊员评价
		},

		situation: '', //患者情况
		suggestion: '', //医嘱
		memberEva: '', //用户评价
		workerEva: '', //陪诊员评价
	},
	//一键拨号陪诊员
	callWorker() {
		const tel = this.data.orderDetail.workerTel
		console.log(tel);
		wx.makePhoneCall({
		  phoneNumber: tel,
		})
	},

	//获取订单信息
	getOrderDetail() {
		const {
			orderId
		} = this.data
		//没订单编号就跳转到首页
		if (orderId == null || orderId == '') {
			returnBack('/pages/hospital/users', '订单信息错误', 'error')
		} else {
			//去获取订单
			getOrder(orderId).then((res) => {
				console.log(res);
				//把订单赋值给orderDetail
				this.setData({
					orderDetail: res.data[0],
					stepIndex: Number(res.data[0].status) + 1 //当前订单状态的下标
				})
			})
		}
	},

	/**服务流程 */
	//去支付
	pay() {
		wx.navigateTo({
			url: '/pages/pay/pay?orderId=' + this.data.orderId,
		})
	},

	//确认接单
	confirm() {
		const { orderId } = this.data
		changeOrder(orderId, '1').then((res) => {
			console.log(res);
			this.setData({
				orderDetail: res.data[0],
				stepIndex: Number(res.data[0].status) + 1
			})
		})
	},

	//1.开始服务
	start() {
		const { orderId } = this.data
		changeOrder(orderId, '2').then((res) => {
			console.log(res);
			this.setData({
				orderDetail: res.data[0],
				stepIndex: Number(res.data[0].status) + 1
			})
			wx.showToast({
				title: '开始服务',
			})
		})
	},

	//2.完成服务
	//输入患者情况
	situation(e) {
		this.setData({
			situation: e.detail.value
		})
		console.log(this.data.situation);
	},
	//输入医嘱
	suggestion(e) {
		this.setData({
			suggestion: e.detail.value
		})
		console.log(this.data.suggestion);
	},
	//完成提交
	finish() {
		const { orderId,situation,suggestion } = this.data
		finishOrder(orderId, situation, suggestion).then((res) => {
			console.log(res.data);
			this.setData({
				orderDetail: res.data[0],
				stepIndex: Number(res.data[0].status) + 1
			})
		})
	},

	//3.确认完成
	affirm() {
		const { orderId } = this.data
		changeOrder(orderId, '4').then((res) => {
			console.log(res);
			this.setData({
				orderDetail: res.data[0],
				stepIndex: Number(res.data[0].status) + 1
			})
		})
	},

	//4.提交评价
	//改变评分
	onChange(e) {
		const { type } = e.currentTarget.dataset;
		const { value } = e.detail;
		console.log('用户类型', type);
		console.log('用户评分', value);
		if (type == '1') {
			this.setData({
				'orderDetail.memberEva': value,
				memberEva: value
			})
			console.log('用户评分', this.data.memberEva);
			finishEvaluate(this.data.orderId, this.data.memberEva, '1').then((res) => {
				this.setData({
					orderDetail: res.data[0]
				})
			})
		} else if (type == '2') {
			this.setData({
				'orderDetail.workerEva': value,
				workerEva: value
			})
			console.log('陪诊员评分', this.data.workerEva);
			finishEvaluate(this.data.orderId, this.data.workerEva, '2').then((res) => {
				this.setData({
					orderDetail: res.data[0]
				})
			})
		}
	},
	//完成评价
	evaluate() {
		const { orderId } = this.data
		changeOrder(orderId, '5').then((res) => {
			this.setData({
				orderDetail: res.data[0],
				stepIndex: Number(res.data[0].status) + 1
			})
		})
	},

	//* 取消订单/取消接单
	cancel() {
		const { orderId } = this.data
		changeOrder(orderId, '6').then((res) => {
			this.setData({
				orderDetail: res.data[0],
				stepIndex: Number(res.data[0].status) + 1
			})
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(options.orderId);
		//从路由中取到订单号，赋值给orderId
		this.setData({
			orderId: options.orderId,
			currentUser: app.globalData.currentUser
		})
		//获取订单详细信息
		this.getOrderDetail()
	}
})